package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.dto.SysroleDTO;
import com.itheima.advantagesup.entity.PageBean;
import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.entity.Sysrole;
import com.itheima.advantagesup.entity.SysRoleMenu;
import com.itheima.advantagesup.mapper.SysRoleMapper;
import com.itheima.advantagesup.service.SysroleService;
import com.itheima.advantagesup.vo.SysroleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class SysroleServiceImpl implements SysroleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Override
    public PageBean findAllrole(Integer totalPage, Integer pageSize, String name) {
        //设置分页参数
        PageHelper.startPage(totalPage,pageSize);
        //执行查询
       /* Sysrole sysrole=new Sysrole();
        sysrole.setId(sysroleDTO.getId());
        sysrole.setName(sysroleDTO.getName());
        sysrole.setDescription(sysroleDTO.getDescription());
        sysrole.setDelFlag(sysroleDTO.getDel_flag());
        sysrole.setSuperadmin(sysroleDTO.getSuperadmin());
        sysrole.setDefaultData(sysroleDTO.getDefault_data());
        sysrole.setRemarks(sysroleDTO.getRemarks());
        sysrole.setRoleid(sysroleDTO.getRole_id());
        sysrole.setRolemenus(sysroleDTO.getRole_menus());
        sysrole.setCreateDate(LocalDateTime.now());
        sysrole.setUpdateDate(LocalDateTime.now());*/
        System.out.println("mapper->执行");
        List<Sysrole> list=sysRoleMapper.findAllrole(name);
          /*  SysroleVO sysroleVO=new SysroleVO();
            sysroleVO.setId(list.getId());
            sysroleVO.setName(list.getName());
            sysroleVO.setDescription(list.getDescription());
            sysroleVO.setDel_flag(list.getDelFlag());
            sysroleVO.setSuperadmin(list.getSuperadmin());
            sysroleVO.setDefault_data(list.getDefaultData());
            sysroleVO.setRemarks(list.getRemarks());
            sysroleVO.setRole_id(list.getRoleid());
            sysroleVO.setRole_menus(list.getRolemenus());
            sysroleVO.setCreate_date(list.getCreateDate());
            sysroleVO.setUpdate_date(list.getUpdateDate());
        Page<SysroleVO> p=(Page<SysroleVO>)sysroleVO;*/
  /*      List<SysroleVO> sysroleVOS=new ArrayList<>();
        for(Sysrole sysrole1:list){
            SysroleVO sysroleVO=new SysroleVO();
            sysroleVO.setId(sysrole1.getId());
            sysroleVO.setName(sysrole1.getName());
            sysroleVO.setDescription(sysrole1.getDescription());
            sysroleVO.setDel_flag(sysrole1.getDelFlag());
            sysroleVO.setSuperadmin(sysrole1.getSuperadmin());
            sysroleVO.setDefault_data(sysrole1.getDefaultData());
            sysroleVO.setRemarks(sysrole1.getRemarks());
            sysroleVO.setRole_id(sysrole1.getRoleid());
            sysroleVO.setRole_menus(sysrole1.getRolemenus());
            sysroleVO.setCreate_date(sysrole1.getCreateDate());
            sysroleVO.setUpdate_date(sysrole1.getUpdateDate());
            sysroleVOS.add(sysroleVO);
        }

        Page<SysroleVO> p=(Page<SysroleVO>)sysroleVOS;*/
        Page<Sysrole> p=(Page<Sysrole>)list;
        PageBean pageBean=new PageBean(p.getTotal(),p.getPageSize(),p.getPageNum(),p.getPages(),p.getResult());
        return pageBean;
    }

    /**
     * 角色添加
     * @param sysroleDTO
     */
    @Transactional
    @Override
    public void addRole(SysroleDTO sysroleDTO) {
        Sysrole sysrole=new Sysrole();
        BeanUtils.copyProperties(sysroleDTO,sysrole);
        sysrole.setId(UUID.randomUUID().toString());
        sysrole.setCreate_date(LocalDateTime.now());
        sysrole.setUpdate_date(LocalDateTime.now());
        sysrole.setDefaultData("1");
        sysRoleMapper.addRole(sysrole);
        String roleId = sysrole.getId();
        List<String> menuids = sysroleDTO.getMenuids();
        List<SysRoleMenu> list=new ArrayList<>();
        for (String MenuId : menuids) {
            SysRoleMenu sysroles=new SysRoleMenu(roleId,MenuId);
            list.add(sysroles);
        }
        sysRoleMapper.addRoleMenu(list);
    }
    @Transactional
    @Override
    public void delete(String id) {
        sysRoleMapper.delete(id);
        sysRoleMapper.deleteRoleMenu(id);
    }

    @Override
    public SysroleVO getById(String id) {
        Sysrole sysrole=sysRoleMapper.getById(id);
        SysroleVO sysroleVO=new SysroleVO();
        BeanUtils.copyProperties(sysrole,sysroleVO);
        sysroleVO.setRole_menus(sysrole.getRolemenus());
        sysroleVO.setCreate_date(sysrole.getCreate_date());
        sysroleVO.setUpdate_date(sysrole.getUpdate_date());
        sysroleVO.setDefault_data(sysrole.getDefaultData());
        sysroleVO.setRoleId(sysrole.getRole_id());
        sysroleVO.setDel_flag(sysrole.getDelFlag());
        List<SysMenu> list=sysRoleMapper.getByIdmenu(id);
        sysroleVO.setAuthList(list);
        return sysroleVO;
    }

    /**
     * 修改
     * @param sysroleDTO
     */
    @Override
    public void update(SysroleDTO sysroleDTO) {
        Sysrole sysrole=new Sysrole();
        BeanUtils.copyProperties(sysroleDTO,sysrole);
        sysrole.setUpdate_date(LocalDateTime.now());
        sysrole.setDefaultData("1");
        sysRoleMapper.update(sysrole);
        sysRoleMapper.deleteRoleMenu(sysrole.getId());
        String roleId = sysrole.getId();
        List<String> menuids = sysroleDTO.getMenuids();
        List<SysRoleMenu> list=new ArrayList<>();
        for (String MenuId : menuids) {
            SysRoleMenu sysroles=new SysRoleMenu(roleId,MenuId);
            list.add(sysroles);
        }
        sysRoleMapper.addRoleMenu(list);
    }

    @Override
    public List<SysroleDTO> findAll() {
        return sysRoleMapper.findAll();
        }
    }
